package actions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.AdminControlPanel;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import beans.BackupRestoreForm;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.BackupOp;
import model.RestoreOp;

public class BackupRestoreAction extends org.apache.struts.actions.DispatchAction 
{   
	AdminControlPanel acp;
    public BackupRestoreAction() 
    {
    	acp=new AdminControlPanel();
    }
    
    public ActionForward UpLoad(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
    	ActionForward forward= new ActionForward();
    	PrintWriter out=response.getWriter();
    	
    	BackupRestoreForm uploadForm = (BackupRestoreForm)form;
    	FileOutputStream outputStream = null;
    	FormFile formFile = null;
    	try 
    	{
    		formFile = uploadForm.getFile();
    		outputStream = new FileOutputStream(new File("d:\\backup.dmp"));    		
    		outputStream.write(formFile.getFileData());
    	}
    	finally 
    	{
    		if (outputStream != null) 
    		{
    			outputStream.close();
    		}
    	}
    	System.out.println("Upload Completed");
    	forward=mapping.findForward("upload");
    	return forward;
    }
    
    public ActionForward Restore(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
        try {
                File f=new File("C:\\Users\\Sci.Maha\\Desktop\\DForum\\src\\java\\actions\\import.bat");
                PrintWriter out = new PrintWriter(f);
                out.println("@echo off");
                out.println("imp jfourm/iti FULL=y IGNORE=Y FILE=D:/backup.dmp");
                out.print("exit");
                out.close();
                Runtime.getRuntime().exec("C:\\Users\\Sci.Maha\\Desktop\\DForum\\src\\java\\actions\\import.bat");

            } catch (Exception ex) {
                ex.printStackTrace();
            }
    	ActionForward forward= new ActionForward();
//    	RestoreOp ro=new RestoreOp();
//    	ro.start();
    	forward=mapping.findForward("restorerunningforward");
    	return forward;
    }
    
    
    public ActionForward Backup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
        try {
                File f=new File("C:\\Users\\Sci.Maha\\Desktop\\DForum\\src\\java\\actions\\export.bat");
                PrintWriter out = new PrintWriter(f);
                out.println("@echo off");
                out.println("exp USERID=jfourm/iti OWNER=jfourm FILE=D:/backup.dmp");
                out.print("exit");
                out.close();
                Runtime.getRuntime().exec("C:\\Users\\Sci.Maha\\Desktop\\DForum\\src\\java\\actions\\export.bat");

            } catch (Exception ex) {
                ex.printStackTrace();
            }
    	ActionForward forward=new ActionForward();
//    	BackupOp bo=new BackupOp();
//    	bo.start();
    	forward=mapping.findForward("backuprunningforward");
    	return forward;
    }

}